import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios';
import {countryList}from './countryList.js';
// import {countryList2}from './countryList2.js';
import { Toast,Dialog } 	from 'vant';
Vue.use(Toast,Dialog);

export default {
	name: 'besuper',
	data(){
		return {
			// token:'',
			infoEmail:'',			//提醒邮箱
			loading:false,			//按钮是否加载中
			countryShown:false,		//是否显示选择国家
			mediaShown:false,		//是否显示宣传用社交媒体
			name:'',				//姓名
			countryId:'',  			//国家ID
			email:'',				//邮箱
			randCode:'',			//邮箱验证码
			medias:[],				//社交媒体
			mediaLink:'',			//社交媒体账号
			remarks:'',				//宣传计划
			checked:true,			//是否同意协议
			seconds:60,				//倒计时
			successDialog:false,	//是否显示成功弹窗
    		isSuccess:1,            //页面状态 0:待定  1:申请页面  2:审核中  3:驳回   4:已是合伙人
    		enableModifyEmail:false, //邮箱是否可编辑  true 可编辑   false 不可以编辑
    		whatsapp:'',		    //whatsapp账号
		}
	},
	computed:{
		countryName(){
			let find = this.countryList.find(item => item.code === this.countryId.toUpperCase());
			if(find){
				return this.locale === 'zh'?find.name:find.enName
			}else{
				return 'error';
			}
		},
		// locale(){
		// 	return this.$i18n.locale
		// },
		// isNi(){
		// 	let country = util.getCookie('country');
		// 	return (country === 'nigeria' && this.locale === 'en')?true:false;
		// },
		btnStatus(){
			//按钮是否可提交状态
			if(this.enableModifyEmail){
				return (this.name && this.email && this.randCode && this.checked && this.mediaLink)?true:false
			}else{
				return (this.name && this.email && this.checked && this.mediaLink)?true:false
			}
		},
		mediaList(){
	        let _arrZh = [
	            {name:'微信',code:'weixin'},
	            {name:'微博',code:'weibo'},
	            {name:'知乎',code:'zhihu'},
	            {name:'币乎',code:'bihu'},
	            {name:'区块链网站',code:'BlockchainWebsite'},
	            {name:'其他',code:'others'},
	        ];
	        let _arrEn = [
	            {name:'twitter',code:'twitter'},
	            {name:'youtube',code:'youtube'},
	            {name:'line',code:'line'},
	            {name:'discord',code:'discord'},
	            {name:'telegram',code:'telegram'},
	            {name:'facebook',code:'facebook'},
	            {name:'blog',code:'blog'},
	            {name:'kakaotalk',code:'kakaotalk'},
	            {name:'trading view',code:'tradingView'},
	            {name:'others',code:'others'}
	        ];
	        _arrZh.map(item => {
	        	let _find = this.medias.find(ele => ele.code === item.code);
	        	item.disabled = _find?true:false;
	        })
	        _arrEn.map(item => {
	        	let _find = this.medias.find(ele => ele.code === item.code);
	        	item.disabled = _find?true:false;
	        })
	        return this.locale === 'zh'?_arrZh:_arrEn
	    },
		countryList(){
			// if(this.locale ==='zh'){
			// 	this.countryName = '中国';
			// }else if(this.locale === 'en'){
			// 	this.countryName = 'United Kingdom';
			// }else{
			// 	this.countryName = 'Japan';
			// }
			
			this.countryId = this.locale;
			if(util.getCookie('country') === 'nigeria'){
				this.countryId = 'NG';
			}
			if(this.locale != 'zh'){
				countryList.map(item => {
					item.name = item.enName
				})
			}
			return countryList
		},
		// isIphoneX(){
		// 	return util.isIphoneX()
		// },
		// isIphone(){
		// 	return util.client()
		// }
	},
	methods:{
		scrollTop(){
			setTimeout(()=>{
				window.scrollTo(0, 0);
			},100)
		},
		upload(){
			if(!this.btnStatus) return
			if(!this.name){
				Toast(this.$t('apply-key44'));
				return
			}
			if((!this.email || this.email.indexOf('@') == -1) && this.enableModifyEmail){
				Toast(this.$t('apply-key45'));
				return
			}
			if((!this.randCode || isNaN(Number(this.randCode))) && this.enableModifyEmail){
				Toast(this.$t('apply-key46'));
				return
			}
			let medias = [];
			this.medias.forEach(item => {
				medias.push(item.name)
			})
			let params = {
				token:this.token || util.getToken(),
				name:this.name,                     //姓名
	            country: this.countryId,            //国家
	            email: this.email,                  //邮箱
	            verify_code: this.randCode,         //验证码
	            propaganda: this.mediaLink,         //社交媒体账号等
	            plan: this.remarks,                 //宣传计划等
	            media:medias.join(','),
	            accounts:this.whatsapp				//非洲版上传whatsapp账号
			}
			util.fetch(`${util.ajaxUrl2}/applySuperPost`,params).then(res => {
				this.loading = false;
				if(res.code === 'Success'){
					this.successDialog = true;
				}else{
					Toast(res.msg)
				}
			}).catch(error => {
				console.log(error)
			})
		},
		sendSms(){
			//发送验证码
			if(!this.enableModifyEmail) return
			if(!this.email || this.email.indexOf('@') === -1){
				Toast(this.$t('apply-key45'))
	            return
	        }
	        this.countDown();
			util.fetch(`${util.ajaxUrl2}/sendVerifyCode`,{
				token:this.token || util.getToken(),
				email:this.email
			}).then(res => {
				if(res.code === 'Success'){
					//发送成功
				}else{
					Toast(res.msg)
				}
			}).catch(error => {
				console.log(error)
			})
		},
		deleteMedia(code){
			//删除媒体
			this.medias.map((item,index,arr) => {
				let _findIndex = arr.findIndex(ele => ele.code === code);
				if(_findIndex > -1){
					this.medias.splice(_findIndex,1)
				}
			})
		},
		selectMedia(item){
			this.medias.push(item)
			this.mediaShown = !this.mediaShown;
		},
		selectCountry(item){
			this.countryId = item.code;
			this.countryShown = !this.countryShown;
		},
		countDown(){
			//倒计时
			this.seconds--;
			if(this.seconds > 0){
				setTimeout(() => {
					this.countDown();
				},1000)
			}else{
				this.seconds = 60;
			}
		},
		init(){
			//检测申请状态
			util.fetch(`${util.ajaxUrl2}/apply`,{
				token:this.token || util.getToken(),
			}).then(res => {
				if(this.locale != 'ja') this.infoEmail = res.email;
				this.enableModifyEmail = res.data.enableModifyEmail;
				//非中文，强制不可编辑
				if(this.locale != 'zh'){
					this.enableModifyEmail = false;
				}
				if(res.code === 'Success'){
				    //查询成功后，判断是否可编辑
				    this.isSuccess = 1;
				}else if(res.code === 'IsSuperPartner'){
				    this.isSuccess = 4;
				}else{
					Toast(res.msg);
				}
				if(res && res.data && res.data.apply){
				    let check = res.data.apply.Check;
				    if(check === 0){
				        //审核中
				        this.isSuccess = 2;
				        this.successDialog = true;
				    }else if(check === 1){
				        //通过
				        // this.isSuccess = 4;
				        //普通合伙人(需要申请)
				        this.isSuccess = 1;
				    }else if(check === 2){
				        //拒绝
				        this.isSuccess = 3;
				        //赋值之前的信息
				        this.name = res.data.apply.Name;
				        this.countryId = res.data.apply.Country;
				        this.email = res.data.apply.Email;
				        this.mediaLink = res.data.apply.Propaganda;
				        this.remarks = res.data.apply.Plan;
				    }
				}
			}).catch(error => {
				console.log(error)
			})
			//拉取用户信息
			util.fetch(`${util.ajaxUrl}/user/verified-info`,{
				token:this.token || util.getToken()
			}).then(res => {
				if(res.retCode === 0){
					this.email = res.retData.email;
				}else{
					Toast(res.retMsg || this.$t('noError'))
				}
			}).catch(error => {
				console.log(error)
			})
		},
		goBack(){
			util.navigateBack(-1,this)
		},
		goAgreement(){
			//跳转到代理协议
			// let _url = '';
			// if(this.locale === 'zh'){
			// 	_url = 'https://support.deepcoin.pro/hc/zh-cn/articles/360047160992-%E4%BB%A3%E7%90%86%E7%94%A8%E6%88%B7%E5%8D%8F%E8%AE%AE';
			// }else if(this.locale === 'en'){
			// 	_url = 'https://support.deepcoin.pro/hc/en-001/articles/360048193991-Deepcoin-Affiliate-Program-Agreement';
			// }else{
			// 	_url = 'https://support.deepcoin.pro/hc/ja/articles/360048193991-Deepcoin-%E3%82%A2%E3%83%95%E3%82%A3%E3%83%AA%E3%82%A8%E3%82%A4%E3%83%88%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%A0%E5%88%A9%E7%94%A8%E8%A6%8F%E7%B4%84';
			// }
			let _url = 'https://support.deepcoin.com/hc/zh-cn/articles/360048193991-%E4%BB%A3%E7%90%86%E7%94%A8%E6%88%B7%E5%8D%8F%E8%AE%AE';
	        if(this.locale === 'en'){
	            _url = 'https://support.deepcoin.com/hc/en-001/articles/360048193991-Deepcoin-Affiliate-Program-Agreement'
	        }else if(this.locale === 'ja'){
	            _url = 'https://support.deepcoin.com/hc/ja/articles/360048193991-Deepcoin-%E3%82%A2%E3%83%95%E3%82%A3%E3%83%AA%E3%82%A8%E3%82%A4%E3%83%88%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%A0%E5%88%A9%E7%94%A8%E8%A6%8F%E7%B4%84';
	        }
			if(util.inBBJ()){
				util.callHandler('openUrl',{
					url:_url,
					title:'',
					needLogin:false,
					popVC:false,		//跳转的时候是否干掉当前页面
					hiddenNav:false,	//是否全屏
				},res => {
					console.log(res)
				})
			}else{
				window.location.href = _url;
			}
		},
		onChange(){
			this.checked = !this.checked;
		},
		// stop(){
		// 	return
		// },
		goHome(){
			let from = this.$route.query.from;
			this.successDialog = false;
			if(from && from === 'home'){
				//退出H5
				util.navigateBack(-1,this);
			}else{
				this.$router.replace({
					path:'/invite',
					query:{
						death:1
					}
				})
			}
		}
	},
	created() {
		util.waitSDKReady().then(token => {
			this.token = token;
			this.init();
		}).catch((e) => {
			// Toast('获取token失败')
		})
		setTimeout(() => {
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				localStorage.removeItem('token');
			})
			util.callHandler('setStatusBarStyle',{
				style:'dark'
			})
		},500)
    }
}